#include "slots.h"
using namespace std;

Slots::Slots(){
		slots = new Slot*[5];
		for (int p = 0; p < 5;p++){
			slots[p] = new Slot;
			slots[p] -> active = false; 
			slots[p] -> name    = "";
			slots[p] -> message = "";
			slots[p] -> change = false;
			slots[p] -> cursor = -1;
		}
		clientnr = -1;
}
Slots::~Slots(){
		for (int p = 0; p <5;p++){
			delete slots[p];
		}
		delete[] slots;
}
int Slots::changed() const{
		int i = 0;
		while(! slots[i]->change){
			i++;
		}
		return i;
}
string Slots::first_message()const{
		// # | 1 n a m e INT | 2 n a m e b i s INT | ... || text
		// 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ... 
		string firstmessage;
		/*firstmessage += '0' + numberof();
		for(int i=0;i< numberof()-1;i++){
			firstmessage += '|';
			firstmessage += '0'+i+1;
			firstmessage += whois(i);
		}
		
		firstmessage+= '|';
		firstmessage+= '|';*/
		firstmessage= "/nint main\(){/treturn 0;/n}/n";
		return firstmessage;
}
void Slots::new_client(){
		// Doesn't check wether one is free or not!!
		int p=0;
		while(p<5 && slots[p] -> active){
			p++;
			cout << "Slot is active, so we won't take this one: nr "<< p << " name: "<< slots[p] -> name << endl;
		}
		slots[p] -> active  = true;
		slots[p] -> name 	+= '0'+p;
		slots[p] -> change  = true;
		slots[p] -> message = first_message();
		clientnr = p;
}
string Slots::showmessage(){
		slots[clientnr] -> change = false;
		string message = slots[clientnr]->message;
		slots[clientnr] -> message = "blublu";
		return message;
}
int Slots::numberof()const{
		int count = 0;
		for(int i=0; i < 5; i++){
			if(slots[i]->active) count++;
		}
		return count;
}
int Slots::clientid() const{
		return clientnr;
}
string Slots::whois() const{
		return slots[clientnr] -> name;
}
string Slots::whois(int clnr) const{
		return slots[clnr-1] -> name;
}
void Slots::changename(string _name){
		slots[clientnr] -> name = _name;
}
